class Program < ActiveRecord::Base

  has_many :sites

  validates_presence_of :name
  # validates_length_of :website, :within => 10..100

  def percent_complete
    return calc_percent_complete(distance_cache, goal_cache)
  end

  def update_caches!
    distance = 0
    sessions = 0
    goal     = 0
    runners  = 0
    groups   = 0
    sites.each do |site|
      distance = distance + site.distance_cache
      sessions = sessions + site.sessions_cache
      goal     = goal     + site.goal_cache
      runners  = runners  + site.runners_cache
      groups   = groups   + site.groups_cache
    end
    self.distance_cache = distance
    self.sessions_cache = sessions
    self.goal_cache     = goal
    self.runners_cache  = runners
    self.groups_cache   = groups
    self.sites_cache    = sites.count
    self.save!
    logger.debug{">>> UPDATED CACHES FOR PROGRAM: #{name}"}
  end

end
